package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lljjcoder.citypickerview.widget.CityPicker;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.SuccessBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.bean.UserAddressBean;
import cn.edu.neusoft.wuguangsheng.flowerimpression.listener.TListener;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.AddUserAddressModel;
import cn.edu.neusoft.wuguangsheng.flowerimpression.model.GetUserAddressByIdModel;

public class UpdateAddressActivity extends BaseActivity {
    ImageView updateaddress_fh;
    EditText updateaddress_username,updateaddress_phone,updateaddress_shengshi,updateaddress_youbian,updateaddress_address;
    TextView updateaddress_xzdz;
    Button updateaddress_btn;
    String useraddressid;
    private CityPicker cityPicker;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_update_address);
        updateaddress_fh = (ImageView)findViewById(R.id.updateaddress_fh);
        updateaddress_username = (EditText)findViewById(R.id.updateaddress_username);
        updateaddress_phone = (EditText)findViewById(R.id.updateaddress_phone);
        updateaddress_shengshi = (EditText)findViewById(R.id.updateaddress_shengshi);
        updateaddress_youbian = (EditText)findViewById(R.id.updateaddress_youbian);
        updateaddress_address = (EditText)findViewById(R.id.updateaddress_address);
        updateaddress_xzdz = (TextView)findViewById(R.id.updateaddress_xzdz);
        updateaddress_btn = (Button)findViewById(R.id.updateaddress_btn);
        useraddressid = getIntent().getStringExtra("useraddressid");
        Toast.makeText(UpdateAddressActivity.this,useraddressid,Toast.LENGTH_SHORT).show();

        GetUserAddressByIdModel getUserAddressByIdModel = new GetUserAddressByIdModel();
        getUserAddressByIdModel.getUserAddressById(useraddressid, new TListener<UserAddressBean>() {
            @Override
            public void onResponse(UserAddressBean userAddressBean) {
                updateaddress_username.setText(userAddressBean.getUseraddress_username());
                updateaddress_phone.setText(userAddressBean.getUseraddress_phone());
                updateaddress_shengshi.setText(userAddressBean.getUseraddress_city());
                updateaddress_youbian.setText(userAddressBean.getUseraddress_youbian());
                updateaddress_address.setText(userAddressBean.getUseraddress_address());
            }

            @Override
            public void onFail(String msg) {

            }
        });
    }
    @Override
    void initEvent() {
        updateaddress_xzdz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initCityPicker();
                cityPicker.show();
            }
        });
        updateaddress_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = updateaddress_username.getText().toString().trim();
                String phone = updateaddress_phone.getText().toString().trim();
                String city = updateaddress_shengshi.getText().toString().trim();
                String address = updateaddress_address.getText().toString().trim();
                String youbian = updateaddress_youbian.getText().toString().trim();

                AddUserAddressModel addUserAddressModel = new AddUserAddressModel();
                addUserAddressModel.updateUserAddress(useraddressid,username,phone,youbian,city,address,new TListener<SuccessBean>() {
                    @Override
                    public void onResponse(SuccessBean successBean) {
                        if (successBean.getSuccess().equals("1")){
                            Toast.makeText(UpdateAddressActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                            finish();
                        }
                        else {
                            Toast.makeText(UpdateAddressActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFail(String msg) {
                        Toast.makeText(UpdateAddressActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }

    @Override
    void initDate() {

    }

    public void initCityPicker() {

        //滚轮文字的大小
//滚轮文字的颜色
//省份滚轮是否循环显示
        //城市滚轮是否循环显示
//地区（县）滚轮是否循环显示
        //滚轮显示的item个数
        //滚轮item间距
        cityPicker = new CityPicker.Builder(UpdateAddressActivity.this)
                .textSize(20)//滚轮文字的大小
                .title("地址选择")
                .backgroundPop(0xa0000000)
                .titleBackgroundColor("#0CB6CA")
                .titleTextColor("#000000")
                .backgroundPop(0xa0000000)
                .confirTextColor("#000000")
                .cancelTextColor("#000000")
                .province("xx省")
                .city("xx市")
                .district("xx区")
                .textColor(Color.parseColor("#000000"))//滚轮文字的颜色
                .provinceCyclic(true)//省份滚轮是否循环显示
                .cityCyclic(false)//城市滚轮是否循环显示
                .districtCyclic(false)//地区（县）滚轮是否循环显示
                .visibleItemsCount(7)//滚轮显示的item个数
                .itemPadding(10)//滚轮item间距
                .onlyShowProvinceAndCity(false)
                .build();

//监听方法，获取选择结果
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                //省份
                String province = citySelected[0];
                //城市
                String city = citySelected[1];
                //区县（如果设定了两级联动，那么该项返回空）
                String district = citySelected[2];
                //邮编
                String code = citySelected[3];

                updateaddress_shengshi.setText(province + city + district);
                updateaddress_youbian.setText(code);
                // Log.e("aaaaaaaaaaaaaa",adresss.getText().toString());
            }
            @Override
            public void onCancel() {


            }
        });
    }
}
